<template>
	<div class="node-wrap">
		<div class="node-wrap-box" @click="show">
			<div class="title" style="background: #3296fa;">
				<el-icon class="icon"><el-icon-promotion /></el-icon>
				<span>{{ nodeConfig.nodeName }}</span>
				<el-icon class="close" @click.stop="delNode()"><el-icon-close /></el-icon>
			</div>
			<div class="content">
				<span v-if="toText(nodeConfig)">{{ toText(nodeConfig) }}</span>
				<span v-else class="placeholder">请选择人员</span>
			</div>
		</div>
		<add-node v-model="nodeConfig.childNode" :parent-data="nodeConfig"></add-node>
		<el-drawer title="抄送人设置" v-model="drawer" destroy-on-close append-to-body :size="500">
			<template #header>
				<div class="node-wrap-drawer__title">
					<label @click="editTitle" v-if="!isEditTitle">{{form.nodeName}}<el-icon class="node-wrap-drawer__title-edit"><el-icon-edit /></el-icon></label>
					<el-input v-if="isEditTitle" ref="nodeTitle" v-model="form.nodeName" clearable @blur="saveTitle" @keyup.enter="saveTitle"></el-input>
				</div>
			</template>
			<el-container>
				<el-main style="padding:0 20px 20px 20px">
					<el-form label-position="top">
						<el-tabs model-value="first" class="demo-tabs">
							<el-tab-pane label="基础设置" name="first">
								<el-form-item label="抄送人员类型">
									<el-radio-group v-model="form.setType">
										<el-radio :label="1">指定成员</el-radio>
										<el-radio :label="2">部门主管</el-radio>
										<el-radio :label="3">指定角色</el-radio>
										<el-radio :label="0">发起人</el-radio>
									</el-radio-group>
								</el-form-item>

								<el-form-item v-if="form.setType==1" label="选择成员">
									<el-button type="primary" icon="el-icon-plus" round @click="selectHandle(1, form.nodeUserList)">选择人员</el-button>
									<div class="tags-list">
										<el-tag v-for="(user, index) in form.nodeUserList" :key="user.id" closable @close="delUser(index)" style="margin-right: 5px;font-size: 14px;">{{user.name}}</el-tag>
									</div>
								</el-form-item>

								<el-form-item v-if="form.setType==2" label="指定主管">
									发起人的第<el-input-number v-model="form.examineLevel" :min="1" style="margin: 0px 5px;"/>级主管
								</el-form-item>

								<el-form-item v-if="form.setType==3" label="选择角色">
									<el-button type="primary" icon="el-icon-plus" round @click="selectHandle(2, form.nodeRoleList)">选择角色</el-button>
									<div class="tags-list">
										<el-tag v-for="(role, index) in form.nodeRoleList" :key="role.id" type="info" closable @close="delRole(index)" style="margin-right: 5px;font-size: 14px;">{{role.name}}</el-tag>
									</div>
								</el-form-item>

								<el-form-item v-if="form.setType==4" label="发起人自选">
									<el-radio-group v-model="form.selectMode">
										<el-radio :label="1">自选一个人</el-radio>
										<el-radio :label="2">自选多个人</el-radio>
									</el-radio-group>
								</el-form-item>

								<el-form-item v-if="form.setType==6" label="连续主管审批终点">
									<el-radio-group v-model="form.directorMode">
										<el-radio :label="0">直到最上层主管</el-radio>
										<el-radio :label="1">自定义审批终点</el-radio>
									</el-radio-group>
									<p v-if="form.directorMode==1">直到发起人的第  <el-input-number v-model="form.directorLevel" :min="1"/> 级主管</p>
								</el-form-item>
							</el-tab-pane>
						</el-tabs>

<!--						<el-form-item label="">
							<el-checkbox v-model="form.userSelectFlag" label="允许发起人自选抄送人"></el-checkbox>
						</el-form-item>-->
					</el-form>
				</el-main>
				<el-footer>
					<el-button type="primary" @click="save">保存</el-button>
					<el-button @click="drawer=false">取消</el-button>
				</el-footer>
			</el-container>
		</el-drawer>
	</div>
</template>

<script>
	import addNode from './addNode'

	export default {
		inject: ['select'],
		props: {
			modelValue: { type: Object, default: () => {} }
		},
		components: {
			addNode
		},
		data() {
			return {
				nodeConfig: {},
				drawer: false,
				isEditTitle: false,
				form: {}
			}
		},
		watch:{
			modelValue(){
				this.nodeConfig = this.modelValue
			}
		},
		mounted() {
			this.nodeConfig = this.modelValue
		},
		methods: {
			show(){
				this.form = {}
				this.form = JSON.parse(JSON.stringify(this.nodeConfig))
				this.drawer = true
			},
			editTitle(){
				this.isEditTitle = true
				this.$nextTick(()=>{
					this.$refs.nodeTitle.focus()
				})
			},
			saveTitle(){
				this.isEditTitle = false
			},
			save(){
				this.$emit("update:modelValue", this.form)
				this.drawer = false
			},
			delNode(){
				this.$emit("update:modelValue", this.nodeConfig.childNode)
			},
			delUser(index){
				this.form.nodeUserList.splice(index, 1)
			},
			delRole(index){
				this.form.nodeRoleList.splice(index, 1)
			},
			selectHandle(type, data){
				this.select(type, data)
			},
			toText(nodeConfig){
				if (nodeConfig.nodeUserList && nodeConfig.nodeUserList.length>0) {
					const users = nodeConfig.nodeUserList.map(item=>item.name).join("、")
					return users
				}else if (nodeConfig.nodeRoleList && nodeConfig.nodeRoleList.length>0){
					const roles = nodeConfig.nodeRoleList.map(item=>item.name).join("、")
					return roles
				}else if (nodeConfig.setType == 2){
					return "发起人的"+this.form.examineLevel+"级主管"
				}else if (nodeConfig.setType == 0){
				    return "抄送给发起人"
				} else{
					if(nodeConfig.userSelectFlag){
						return "未配置抄送人"
					}else{
						return false
					}

				}
			}
		}
	}
</script>

<style>
</style>
